<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    /* 清除默认样式 */
    * {
      margin: 0;
      padding: 0;
    }
    ul,ol {
      list-style: none;
    }
    .lunbo {
      position: relative;
      width: 880px;
      height: 550px;
      border: 1px solid #333;
      margin: 20px auto;
    }
    .lunbo .pic li {
      position: absolute;
      left: 0;
      top: 0;
      width: 880px;
      height: 550px;
      display: none;
    }
    .lunbo .pic .current {
      display: block;
    }
    /* 按钮 */
    .lunbo .btn a {
      position: absolute;
      top: 50%;
      width: 40px;
      height: 40px;
      margin-top: -20px;
      background-color: rgba(255,255,255,0.3);
      font: 20px/40px "宋体";
      text-align: center;
      color: #fff;
      text-decoration: none;
      font-weight: bold;
    }
    .lunbo .btn .leftbtn {
      left: 20px;
    }
    .lunbo .btn .rightbtn {
      right: 20px;
    }
    .lunbo .btn a:hover {
      background-color: rgba(255,255,255,0.6);
    }
    /* 下标 */
    .lunbo .sub {
      position: absolute;
      left: 100px;
      bottom: 50px;
      width: 240px;
    }
    .lunbo .sub li {
      float: left;
      width: 20px;
      height: 20px;
      margin-right: 20px;
      background-color: rgba(255,255,255,0.4);
      font: 12px/20px "微软雅黑";
      text-align: center;
      color: #666;
      cursor: pointer;
    }
    .lunbo .sub .current {
      background-color: #fff;
    }
  </style>
</head>
<body>
  <!-- 轮播图大结构 -->
  <div class="lunbo" id="lunbo">
    <!-- 图片部分 -->
    <ul class="pic" id="ul">
      <li class="current"><img src="images/lunbo/c1.jpg"></li>
      <li><img src="images/lunbo/c2.jpg"></li>
      <li><img src="images/lunbo/c3.jpg"></li>
      <li><img src="images/lunbo/c4.jpg"></li>
      <li><img src="images/lunbo/c5.jpg"></li>
      <li><img src="images/lunbo/c6.jpg"></li>
    </ul>
    <!-- 按钮 -->
    <div class="btn">
      <a href="javascript:;" class="leftbtn" id="leftbtn">&lt;</a>
      <a href="javascript:;" class="rightbtn" id="rightbtn">&gt;</a>
    </div>
    <!-- 小圆点 -->
    <ol class="sub" id="ol">
      <li class="current">1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li>5</li>
      <li>6</li>
    </ol>
  </div>
  <script>
    // 思路：1.设置左右按钮点击切换
    // 2.设置点击小按钮切换到对应的图片上
    // 3.设置定时器轮播

    // 获取元素
    var leftbtn = document.getElementById("leftbtn");
    var rightbtn = document.getElementById("rightbtn");
    var ul = document.getElementById("ul");
    var ulLis = ul.children;
    var ol = document.getElementById("ol");
    var olLis = ol.children;
    var lunbo = document.getElementById("lunbo");

    // 定义全局信号量，接收增加和减少的下标
    var lix = 0;

    // 右按钮事件
    rightbtn.onclick = function () {
     rightMove();
    };
    // 左按钮事件
    leftbtn.onclick = function () {
      // 信号量自减
      lix--;
      console.log(lix);
      if (lix < 0) {
        lix = ulLis.length - 1;
      }
      Move();
    };
    // 小图标点击事件
    for (var i = 0; i < olLis.length ; i++) {
      // 把下标存储到数组里
      olLis[i].index = i;
      console.log(olLis[i].index);
      // 点击的时候，把下标传给信号量，点中的 i 就把index值赋给lix然后传入 Move函数中
      olLis[i].onclick = function () {
          lix = this.index;
          Move();
      };
    }

    // 设置定时切换事件,设置timer为了停止变换
    var timer;
    // 此处可以用右键切换的代码
    timer = setInterval(rightMove,2000);
    // 设置鼠标移上事件
    lunbo.onmouseover = function () {
      console.log(timer);
      clearInterval(timer);
    };
    lunbo.onmouseout = function () {
      timer = setInterval(rightMove,2000);
    };

    // 把向右移动也改写为函数
    function rightMove () {
      // 信号量自加
      lix++;     
      // console.log(lix);
      // 如果超过了最大的列表下标，就要从头开始，让图片循环显示，后面要封装成函数
      if (lix > ulLis.length - 1) {
        lix = 0;          
      }
      Move();
    }

    function Move () {
        // 排他方法：将其他的全部排除，设置自己的样式
        for (var i = 0; i < ulLis.length ; i++) {
                   ulLis[i].className = "";
                   olLis[i].className = "";
                 }
                 ulLis[lix].className = "current";
                 olLis[lix].className = "current"; 
    }
  </script>
</body>
</html>